#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<cstdlib>//rand()
#define rep(i,a,b) for(register int i = (a);i <= (b);++i)
#define per(i,a,b) for(register int i = (a);i >= (b);--i)
typedef long long ll;
typedef unsigned long long ull;
using std::string;using std::cin;using std::cout;

int _,n,p,a[111111],head,tail;
bool flag;

int main(){
    std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen("in.in", "r", stdin);
    cin >> _;
    while(_--){
        flag = true;
        cin >> n >> p;
        rep(i,1,n) cin >> a[i];
        rep(i,1,n) if(a[i] % p != 0) flag = false;
        if(flag) cout << "-1\n";
        else {
            head = tail = 0;
            int sum = 0;
            rep(i,1,n) sum += a[i];
            if(sum % p == 0){
                rep(i,1,n){
                    if(a[i] % p == 0) ++head;
                    else break;
                }
                per(i,n,1){
                    if(a[i] % p == 0) ++tail;
                    else break;
                }
                cout << n - std::min(head,tail) - 1 << "\n";
            } else cout << n << "\n";
        }
    }
    return 0;
}   